# --- Code to generate descriptive stats and reduced form evidence for:
# --- "Estimating an auction platform game with two-sided entry" --- %
# --- Current version: July 2024 --- %
      
      rm(list=ls())
      set.seed(42)
      
      # Make sure pwd is in Marra2024_JPE_Replication. If saved in Dropbox:
      setwd("~/Dropbox/Marra2024_JPE_Replication/")
      
      # Load packages
      require(stargazer)#
      require(xtable)#
      require(dplyr)#

      
      # =========================================================================== %
      # Reproduce tables 2-4 with descriptive stats
      # =========================================================================== %
       
      # Load data 
      # Auction-level data
      df <- read.csv("Code/BFWauctiondata.csv")

      # Potential seller data
      pot <- read.csv("Code/BFWpotentialsellerdata.csv")
      pot[is.na(pot)] <- 0
      
    
      
      ##################################### 
      # Table 2 -- Auction-level descriptive statistics
      
      covlab<-c("Hammer price","Number of bidders","Number bottles","Is sold", "Price per bottle if sold","Sold in Bond",
                "Seller has feedback","Seller has ratings","Reserve price")
      df.table <- data.frame(df$highestbid,
                             df$numberbidders,
                             df$numberbottles,
                             df$sold,
                             df$price,
                             df$inbond,
                             df$hasfeedback,
                             df$hasratings,
                             df$reserve)
      stargazer(df.table,type="latex",
                covariate.labels=covlab,
                digits=2,
                float=FALSE,
                median=TRUE,
                out="Output/Table2.tex")
  
      
      
      ##################################### 
      # Table 3 -- Seller-level descriptive statistics
      
      sellerhet <- df %>% group_by(sellerid) %>% filter(!(sellerid==1)) %>% # Only taking out BFW 
        mutate(NrListingZeror = sum(1-hasreserve),NrListingPosr=sum(hasreserve),Hasreserve=NrListingPosr>0) %>% 
        mutate(Nrsold=sum(sold),
               Shareposr_ifmultiple=ifelse(n()>1,sum(hasreserve)/n(),NA),
               Sharesecret_ifmultiplepos=ifelse(n()>1&&sum(hasreserve)>0,sum(hassecret)/sum(hasreserve),NA),
               Switches_reserve=ifelse(n()>1,max(hasreserve)!=min(hasreserve),NA),
               MeanNrWords=mean(desc_numberwords*10),
               CollectProb=mean(canbecollected),
               NumberBottles=mean(numberbottles),
               NumberBottles_ifmultiple=ifelse(n()>1,mean(numberbottles),NA)) %>% ungroup() %>%
        select(MeanNrWords,CollectProb,Nrsold,NumberBottles,NumberBottles_ifmultiple,Shareposr_ifmultiple,Sharesecret_ifmultiplepos) %>% ungroup() %>% distinct()
      stargazer(data.frame(sellerhet),summary=TRUE,median=TRUE,out="Output/Table3.tex",
                covariate.labels=c("Average number of words in description","Probability allowing for collection","Number of sold listings",
                                   "Number of bottles per listing","If multiple listings: number of bottles per listing",
                                   "If multiple listings: share with $r>0$","If multiple $r>0$ listings: share with $r$ secret"),digits=2)
      
      
      
      ##################################### 
      # Table 4 -- Thin markets
      # Create Products per month and Products definitions 
      df <- df %>%
        group_by(productmonth) %>% mutate(Tpm = length(auctionid),
                                          totn_pm=sum(numberbidders)) %>%
        group_by(product) %>% mutate(Tp = length(auctionid), Tpm2=Tpm*Tpm) %>% group_by() %>% arrange(auctionid)
      
      # Number of listings product/month
      # Conditional on a product being available... :
      Tpm    <- data.frame(a=df$Tpm,b=df$productmonth)
      Tpm    <- Tpm[!duplicated(Tpm$b),]
      q1= quantile(Tpm$a,seq(0.1,1,0.1))
         
      # Number of listings product / full sample
      Tp     <- data.frame(a=df$Tp,b=df$product)
      Tp     <- Tp[!duplicated(Tp$b),]
      q2= quantile(Tp$a,seq(0.1,1,0.1))  
      
      # Repeat listings same title
      title=as.numeric(factor(df$title, levels=as.character(unique(df$title))))
      q3= quantile(as.data.frame(table(title))$Freq,seq(0.1,1,0.1))
      
      # Save table
      tab3 = xtable(as.data.frame(bind_rows(q1,q2,q3)),align="lcccccccccc")
      tab3
      print(tab3,file="Output/Table4.tex",floating=FALSE)
      
   
      